package com.yixin.my.proj.web.controller;

import com.yixin.my.proj.common.PageInfo;
import com.yixin.my.proj.domain.Log;
import com.yixin.my.proj.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "log")
public class LogController {
    @Autowired
    private LogService logService;

    @RequestMapping(value = "list",method = RequestMethod.GET)
    public String log(Model model){
        //List<Log> logs = logService.selectAll();
        //model.addAttribute("log",logs);
        return "/log/list";
    }

    @ResponseBody
    @RequestMapping(value = "page",method = RequestMethod.GET)
    public PageInfo<Log> page(HttpServletRequest request){
        String draw = request.getParameter("draw");
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));

        Map<String, Object> params = new HashMap<>();
        params.put("page", start);
        params.put("pageSize", length);

        PageInfo<Log> pageInfo = logService.page(params);
        pageInfo.setDraw(draw == null ? 0 : Integer.parseInt(draw));

        return pageInfo;
    }

}
